home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP5.TXT < prev    next >
Text File  |  1990-08-08  |  35KB  |  785 lines

  1.  
  2.             Chapter 5 - Functions, variables, and prototypes
  3.  
  4.  
  5.                       OUR FIRST USER DEFINED FUNCTION
  6.  
  7.              Load  and examine the file SUMSQRES.C for an example of
  8.         a C program with functions.   Actually this is not the first
  9.         function  we have encountered because the "main" program  we
  10.         have been using all along is technically a function,  as  is
  11.         the  "printf" function.   The "printf" function is a library
  12.         function that was supplied with your compiler.
  13.  
  14.              Notice the executable part of this program which begins
  15.         in  line  8.   It  begins  with  a  line  that  simply  says
  16.         "header()",  which  is the way to call  any  function.   The
  17.         parentheses are required because the C compiler uses them to
  18.         determine  that  it  is a function call  and  not  simply  a
  19.         misplaced variable.  When the program comes to this line  of
  20.         code, the function named "header" is called, its  statements
  21.         are executed, and control returns to the statement following
  22.         this call.  Continuing on we come to a "for" loop which will
  23.         be  executed 7 times and which calls another function  named
  24.         "square" each time through the loop, and finally a  function
  25.         named "ending" will be called and executed.  For the  moment
  26.         ignore  the  "index"  in  the parentheses  of  the  call  to
  27.         "square".  We have seen that this program therefore calls  a
  28.         header, 7 square calls, and an ending. Now we need to define
  29.         the functions.
  30.  
  31.                            DEFINING THE FUNCTIONS
  32.  
  33.              Following the main program you will see another program
  34.         that  follows all of the rules set forth so far for a "main"
  35.         program  except that it is named "header()".   This  is  the
  36.         function which is called from within the main program.  Each
  37.         of  these  statements are executed,  and when they  are  all
  38.         complete, control returns to the main program.
  39.  
  40.              The  first  statement sets the variable "sum" equal  to
  41.         zero because we will use it to accumulate a sum of  squares.
  42.         Since  the  variable  "sum" is defined as  an  integer  type
  43.         variable  prior to the main program,  it is available to  be
  44.         used  in  any of the following functions.   It is  called  a
  45.         "global" variable,  and it's scope is the entire program and
  46.         all  functions.   More  will  be  said about  the  scope  of
  47.         variables near the end of this chapter.  The next  statement
  48.         outputs  a header message to the monitor.   Program  control
  49.         then  returns  to  the  main  program  since  there  are  no
  50.         additional statements to execute in this function.
  51.  
  52.              It should be clear to you that the two executable lines
  53.         from  this  function  could be moved to  the  main  program,
  54.         replacing the header call,  and the program would do exactly
  55.         the same thing that it does as it is now written.  This does
  56.  
  57.  
  58.                                   Page 31
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.             Chapter 5 - Functions, variables, and prototypes
  69.  
  70.  
  71.         not minimize the value of functions,  it merely  illustrates
  72.         the operation of this simple function in a simple way.   You
  73.         will find functions to be very valuable in C programming.
  74.  
  75.                        PASSING A VALUE TO A FUNCTION
  76.  
  77.              Going  back  to the main program,  and the  "for"  loop
  78.         specifically,  we find the new construct from the end of the
  79.         last lesson used in the last part of the "for" loop,  namely
  80.         the  "index++".  You should get used to seeing this, as  you
  81.         will see it a lot in C programs.
  82.  
  83.              In the call to the function "square",  we have an added
  84.         feature, namely the variable "index" within the parentheses.
  85.         This  is  an indication to the compiler that when you go  to
  86.         the function,  you wish to take along the value of index  to
  87.         use in the execution of that function.  Looking ahead at the
  88.         function  "square",  we  find that another variable name  is
  89.         enclosed in its parentheses,  namely the variable  "number".
  90.         This  is  the name we prefer to call the variable passed  to
  91.         the  function when we are in the function.   We can call  it
  92.         anything  we wish as long as it follows the rules of  naming
  93.         an identifier.   Since the function must know what type  the
  94.         variable  is,  it is defined following the function name but
  95.         before the opening brace of the function itself.   Thus, the
  96.         line  containing "int number;" tells the function  that  the
  97.         value  passed to it will be an integer type variable.   With
  98.         all of that out of the way,  we now have the value of  index
  99.         from  the main program passed to the function "square",  but
  100.         renamed "number", and available for use within the function.
  101.         This  is the "classic" style of defining function  variables
  102.         and has been in use since C was originally defined.  A newer
  103.         method is gaining in popularity due to its many benefits and
  104.         will be discussed later in this chapter.
  105.  
  106.              Following the opening brace of the function,  we define
  107.         another  variable "numsq" for use only within  the  function
  108.         itself,  (more  about  that  later)  and  proceed  with  the
  109.         required  calculations.   We set "numsq" equal to the square
  110.         of  number,  then add numsq to the current total  stored  in
  111.         "sum".   Remember  that "sum += numsq" is the same as "sum =
  112.         sum + numsq" from the last lesson.   We print the number and
  113.         its square, and return to the main program.
  114.  
  115.                   MORE ABOUT PASSING A VALUE TO A FUNCTION
  116.  
  117.              When we passed the value of "index" to the function,  a
  118.         little  more  happened  than meets  the  eye.   We  did  not
  119.         actually  pass  the  value  of index  to  the  function,  we
  120.         actually  passed  a  copy of the value.   In  this  way  the
  121.         original value is protected from accidental corruption by  a
  122.  
  123.  
  124.                                   Page 32
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.             Chapter 5 - Functions, variables, and prototypes
  135.  
  136.  
  137.         called  function.   We  could  have  modified  the  variable
  138.         "number" in any way we wished in the function "square",  and
  139.         when we returned to the main program, "index" would not have
  140.         been  modified.   We thus protect the value of a variable in
  141.         the main program from being accidentally corrupted,  but  we
  142.         cannot  return  a value to the main program from a  function
  143.         using this technique.  We will find a well defined method of
  144.         returning  values  to  the main program or  to  any  calling
  145.         function  when we get to arrays and another method  when  we
  146.         get  to pointers.   Until then the only way you will be able
  147.         to  communicate  back to the calling function will  be  with
  148.         global  variables.    We  have  already  hinted  at   global
  149.         variables  above,  and will discuss them in detail later  in
  150.         this chapter.
  151.  
  152.              Continuing  in  the main program,  we come to the  last
  153.         function call, the call to "ending".  This call simply calls
  154.         the last function which has no local variables defined.   It
  155.         prints out a message with the value of "sum" contained in it
  156.         to  end the program.   The program ends by returning to  the
  157.         main  program and finding nothing else to do.   Compile  and
  158.         run this program and observe the output.
  159.  
  160.                         NOW TO CONFESS A LITTLE LIE
  161.  
  162.              I told you a short time ago that the only way to get  a
  163.         value  back to the main program was through use of a  global
  164.         variable,  but  there  is another way which we will  discuss
  165.         after  you load and display the file  named  SQUARES.C.   In
  166.         this  file we will see that it is simple to return a  single
  167.         value  from a called function to the calling function.   But
  168.         once again,  it is true that to return more than one  value,
  169.         we will need to study either arrays or pointers.
  170.  
  171.              In the main program, we define two integers and begin a
  172.         "for"  loop  which  will be executed  8  times.   The  first
  173.         statement of the "for" loop is "y = squ(x);", which is a new
  174.         and rather strange looking construct.  From past experience,
  175.         we  should have no trouble understanding that  the  "squ(x)"
  176.         portion  of  the statement is a call to the  "squ"  function
  177.         taking along the value of "x" as a variable.  Looking  ahead
  178.         to the function itself we find that the function prefers  to
  179.         call  the variable "in" and it proceeds to square the  value
  180.         of  "in" and call the result "square".  Finally, a new  kind
  181.         of  a statement appears, the "return" statement.  The  value
  182.         within  the parentheses is assigned to the  function  itself
  183.         and  is  returned  as a usable value in  the  main  program.
  184.         Thus,  the function call "squ(x)" is assigned the  value  of
  185.         the square and returned to the main program such that "y" is
  186.         then  set  equal  to  that value.   If  "x"  were  therefore
  187.  
  188.  
  189.  
  190.                                   Page 33
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.             Chapter 5 - Functions, variables, and prototypes
  201.  
  202.  
  203.         assigned  the value 4 prior to this call, "y" would then  be
  204.         set to 16 as a result of this line of code.
  205.  
  206.              Another  way  to  think  of this  is  to  consider  the
  207.         grouping  of characters "squ(x)" as another variable with  a
  208.         value  that is the square of "x",  and this new variable can
  209.         be used any place it is legal to use a variable of its type.
  210.         The values of "x" and "y" are then printed out.
  211.  
  212.              To  illustrate  that the grouping of  "squ(x)"  can  be
  213.         thought  of as just another variable,  another "for" loop is
  214.         introduced in which the function call is placed in the print
  215.         statement rather than assigning it to a new variable.
  216.  
  217.              One  last  point must be made,  the  type  of  variable
  218.         returned must be defined in order to make sense of the data,
  219.         but the compiler will default the type to integer if none is
  220.         specified.   If  any  other  type is  desired,  it  must  be
  221.         explicitly defined.   How to do this will be demonstrated in
  222.         the next example program.
  223.  
  224.              Compile  and  run  this program  which  also  uses  the
  225.         "classic" method of defining function variables.
  226.  
  227.                             FLOATING POINT FUNCTIONS
  228.  
  229.              Load the program FLOATSQ.C for an example of a function
  230.         with a floating point type of return.  It begins by defining
  231.         a global floating point variable we will use later.  Then in
  232.         the  "main"  part  of the program,  an integer  is  defined,
  233.         followed  by two floating point variables,  and then by  two
  234.         strange  looking definitions.   The expressions "sqr()"  and
  235.         "glsqr()"  look like function calls and they are.   This  is
  236.         the proper way in C to define that a function will return  a
  237.         value that is not of the type "int", but of some other type,
  238.         in  this case "float".   This tells the compiler that when a
  239.         value  is returned from either of these  two  functions,  it
  240.         will be of type "float".  This is, once again, the "classic"
  241.         method of defining functions.
  242.  
  243.              Now  refer to the function "sqr" near the center of the
  244.         listing and you will see that the function name is  preceded
  245.         by the name "float".   This is an indication to the compiler
  246.         that  this  function will return a value of type "float"  to
  247.         any program that calls it.   The function is now  compatible
  248.         with  the call to it.   The line following the function name
  249.         contains  "float inval;",  which indicates to  the  compiler
  250.         that  the variable passed to this function from the  calling
  251.         program will be of type "float".
  252.  
  253.  
  254.  
  255.  
  256.                                   Page 34
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.             Chapter 5 - Functions, variables, and prototypes
  267.  
  268.  
  269.              The next function,  namely "glsqr",  will also return a
  270.         "float"  type  variable,  but it uses a global variable  for
  271.         input.   It  also does the squaring right within the  return
  272.         statement  and  therefore has no need to define  a  separate
  273.         variable to store the product.
  274.  
  275.              The  overall structure of this program should  pose  no
  276.         problem and will not be discussed in any further detail.  As
  277.         is customary with all example programs, compile and run this
  278.         program.
  279.  
  280.                THERE IS A BUG IN THE FIRST VERSION OF TURBO C
  281.  
  282.              When  you run this program, using version 1.0 of  Turbo
  283.         C, you will find that the function named "sqr()" will return
  284.         a  value of zero.  This is because it receives a  zero  from
  285.         the  calling program.  The program is written correctly  but
  286.         the compiler has a bug in it.  A call to Borland resulted in
  287.         a  fix  by  simply using the  "modern"  method  of  function
  288.         definition rather than the "classic" which has been used  to
  289.         this  point.   As  you  read articles on  C,  you  will  see
  290.         programs  written in the "classic" style, so you need to  be
  291.         capable  of reading them.  It would be  highly  recommended,
  292.         however,  that you learn and use the "modern"  method  which
  293.         will be covered shortly in this tutorial.  We will return to
  294.         this program and show how to fix it so that it will work  in
  295.         spite of the bug.
  296.  
  297.              Note that this bug is fixed in TURBO C version 1.5.
  298.  
  299.                              SCOPE OF VARIABLES
  300.  
  301.              Load the next program,  SCOPE.C,  and display it for  a
  302.         discussion  of the scope of variables in a program.   Ignore
  303.         the 4 statements in lines 2 through 5 of this program for  a
  304.         few moments, and we will discuss them later.
  305.  
  306.              The first variable defined is a global variable "count"
  307.         which  is available to any function in the program since  it
  308.         is defined before any of the functions.   In addition, it is
  309.         always  available  because  it does not come and go  as  the
  310.         program  is  executed.   (That  will  make  sense  shortly.)
  311.         Farther down in the program,  another global variable  named
  312.         "counter"  is  defined  which  is also  global  but  is  not
  313.         available  to the main program since it is defined following
  314.         the main program.  A global variable is any variable that is
  315.         defined  outside of any function.   Note that both of  these
  316.         variables  are sometimes referred to as  external  variables
  317.         because they are external to any functions.
  318.  
  319.  
  320.  
  321.  
  322.                                   Page 35
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.             Chapter 5 - Functions, variables, and prototypes
  333.  
  334.  
  335.              Return  to  the  main  program and  you  will  see  the
  336.         variable  "index"  defined as an integer.   Ignore the  word
  337.         "register" for the moment.   This variable is only available
  338.         within the main program because that is where it is defined.
  339.         In addition, it is an "automatic" variable, which means that
  340.         it  only comes into existence when the function in which  it
  341.         is  contained  is  invoked,  and ceases to  exist  when  the
  342.         function  is  finished.   This  really  means  nothing  here
  343.         because the main program is always in operation,  even  when
  344.         it  gives  control to another function.  Another integer  is
  345.         defined  within  the  "for"  braces,  namely  "stuff".   Any
  346.         pairing  of braces can contain a variable  definition  which
  347.         will  be  valid  and  available only while  the  program  is
  348.         executing statements within those braces.  The variable will
  349.         be  an  "automatic" variable and will cease  to  exist  when
  350.         execution leaves the braces.   This is convenient to use for
  351.         a loop counter or some other very localized variable.
  352.  
  353.                        MORE ON "AUTOMATIC" VARIABLES
  354.  
  355.              Observe  the  function named "head1" in line  26  which
  356.         looks  a  little funny because of "void" being  used  twice.
  357.         The purpose of the uses of the word "void" will be explained
  358.         shortly.  The  function contains a variable  named  "index",
  359.         which  has  nothing  to  do with the  "index"  of  the  main
  360.         program, except that both are automatic variables.  When the
  361.         program  is  not  actually  executing  statements  in   this
  362.         function,  this variable named "index" does not even  exist.
  363.         When "head1" is called, the variable is generated, and  when
  364.         "head1"  completes its task, the variable in  "head1"  named
  365.         "index"  is eliminated completely from existence.   Keep  in
  366.         mind  however that this does not affect the variable of  the
  367.         same  name  in the main program, since it  is  a  completely
  368.         separate entity.
  369.  
  370.              Automatic   variables  therefore,   are   automatically
  371.         generated and disposed of when needed.   The important thing
  372.         to remember is that from one call to a function to the  next
  373.         call,  the  value of an automatic variable is not  preserved
  374.         and must therefore be reinitialized.
  375.  
  376.                          WHAT ARE STATIC VARIABLES?
  377.  
  378.              An  additional variable type must be mentioned at  this
  379.         point,  the "static" variable.  By putting the reserved word
  380.         "static"  in  front  of  a  variable  declaration  within  a
  381.         function,  the variable or variables in that declaration are
  382.         static  variables  and will stay in existence from  call  to
  383.         call  of  the  particular function.
  384.  
  385.  
  386.  
  387.  
  388.                                   Page 36
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.             Chapter 5 - Functions, variables, and prototypes
  399.  
  400.  
  401.              By  putting  the  same reserved word  in  front  of  an
  402.         external variable, one outside of any function, it makes the
  403.         variable  private  and  not accessible to use in  any  other
  404.         file.  This implies that it is possible to refer to external
  405.         variables  in other separately compiled files,  and that  is
  406.         true.  Examples of this usage will be given in chapter 14 of
  407.         this tutorial.
  408.  
  409.                          USING THE SAME NAME AGAIN
  410.  
  411.              Refer  to  the  function named  "head2".   It  contains
  412.         another  definition  of the variable  named  "count".   Even
  413.         though  "count"  has  already  been  defined  as  a   global
  414.         variable,  it  is  perfectly all right to reuse the name  in
  415.         this  function.   It is a completely new variable  that  has
  416.         nothing to do with the global variable of the same name, and
  417.         causes  the  global  variable  to  be  unavailable  in  this
  418.         function.   This allows you to write programs using existing
  419.         functions  without worrying about what names were  used  for
  420.         variables in the functions because there can be no conflict.
  421.         You  only  need to worry about the variables that  interface
  422.         with the functions.
  423.  
  424.                         WHAT IS A REGISTER VARIABLE?
  425.  
  426.              Now  to  fulfill  a promise made earlier about  what  a
  427.         register  variable  is.   A  computer can  keep  data  in  a
  428.         register  or  in  memory.   A  register is  much  faster  in
  429.         operation  than  memory  but there are  very  few  registers
  430.         available for the programmer to use.   If there are  certain
  431.         variables  that are used extensively in a program,  you  can
  432.         designate  that  those  variables  are to  be  stored  in  a
  433.         register  if possible in order to speed up the execution  of
  434.         the program.  Turbo C allows you to use 2 register variables
  435.         and will ignore additional requests if you request more than
  436.         2.
  437.  
  438.              Turbo C allows you to use register variables for  short
  439.         int and int types along with their unsigned counterparts. It
  440.         also  allows  you  to  use register  storage  for  two  byte
  441.         pointers which we will study in about 3 chapters.
  442.  
  443.                         WHERE DO I DEFINE VARIABLES?
  444.  
  445.              Now for a refinement on a general rule stated  earlier.
  446.         When  you have variables brought to a function as  arguments
  447.         to  the function, and you are using the "classic"  style  of
  448.         programming, they are defined immediately after the function
  449.         name and prior to the opening brace for the program.   Other
  450.         variables used in the function are defined at the  beginning
  451.  
  452.  
  453.  
  454.                                   Page 37
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.             Chapter 5 - Functions, variables, and prototypes
  465.  
  466.  
  467.         of the function, immediately following the opening brace  of
  468.         the function, and before any executable statements.
  469.  
  470.                             WHAT IS PROTOTYPING?
  471.  
  472.              A  prototype  is a "model" of the real thing  and  when
  473.         programming  in  Turbo C, you have the ability to  define  a
  474.         "model" of each function for the compiler.  The compiler can
  475.         then  use  the "model" to check each of your  calls  to  the
  476.         function  and determine if you have used the correct  number
  477.         of  arguments  in the function call and if they are  of  the
  478.         correct type.  By using prototypes, you let the compiler  do
  479.         some  additional error checking for you.  The ANSI  standard
  480.         for  C  should be released late in 1987,  and  will  contain
  481.         prototyping as part of its recommended standard.  Every good
  482.         C  compiler will have prototyping available, so  you  should
  483.         learn to use it.
  484.  
  485.              Returning to lines 3, 4, and 5 in SCOPE.C, we have  the
  486.         prototypes  for  the three functions  contained  within  the
  487.         program.   The first "void" in each line tells the  compiler
  488.         that  these particular functions do not return a  value,  so
  489.         that the compiler would flag the statement index =  head1();
  490.         as  an  error because nothing is returned to assign  to  the
  491.         variable  "index".  The word "void" within  the  parentheses
  492.         tells the compiler that this function requires no parameters
  493.         and  if a variable were included, it would be an  error  and
  494.         the  compiler would issue a warning message.  If  you  wrote
  495.         the  statement  head1(index);, it would be  a  error.   This
  496.         allows  you  to use type checking when programming in  C  in
  497.         much the same manner that it is used in Pascal, Modula 2, or
  498.         Ada.
  499.  
  500.              You  should enable prototype checking with Turbo  C  at
  501.         this  point to see how well it works.  In the "Option"  menu
  502.         select  "Compiler", then "Errors", followed by "Less  common
  503.         errors",  then  turn  on  "F:  Call  to  function  with   no
  504.         prototype" by hitting the "F" key.  The program SCOPE.C will
  505.         not produce any warnings, but RECURSON.C which is next  will
  506.         result in some warning messages.
  507.  
  508.              Line 2 of SCOPE.C tells the system to go to the include
  509.         files  and  get the file named STDIO.H  which  contains  the
  510.         prototypes  for the standard input and output  functions  so
  511.         they can be checked for proper variable types.  Don't  worry
  512.         about the "include" yet, it will be covered in detail  later
  513.         in this tutorial.
  514.  
  515.              Compile  and run this program and, when you  return  to
  516.         the  Integrated  Environment,  observe  that  there  are  no
  517.         warning messages.
  518.  
  519.  
  520.                                   Page 38
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.             Chapter 5 - Functions, variables, and prototypes
  531.  
  532.  
  533.  
  534.                          STANDARD FUNCTION LIBRARIES
  535.  
  536.              Every  compiler  comes  with some  standard  predefined
  537.         functions  which  are available for  your  use.   These  are
  538.         mostly   input/output   functions,   character  and   string
  539.         manipulation  functions, and math functions.  We will  cover
  540.         most  of  these  in  subsequent  chapters.   Prototypes  are
  541.         defined  for you by the Turbo C compiler writer for  all  of
  542.         the  functions that are included with your compiler.  A  few
  543.         minutes  spent  studying your Turbo C Reference  Guide  will
  544.         give you an insight in where the prototypes are defined  for
  545.         each of the functions.
  546.  
  547.              In addition,  most compilers have additional  functions
  548.         predefined that are not standard but allow the programmer to
  549.         get the most out of his particular computer.  In the case of
  550.         the  IBM-PC and compatibles,  most of these functions  allow
  551.         the  programmer  to use the BIOS services available  in  the
  552.         operating system, or to write directly to the video  monitor
  553.         or to any place in memory.  These will not be covered in any
  554.         detail as you will be able to study these unique aspects  of
  555.         the  Turbo  C  compiler on your own.   The  entire  Turbo  C
  556.         Reference Guide is devoted to defining functions.   Many  of
  557.         these kinds of functions are used in the example programs in
  558.         chapter 14.
  559.  
  560.                              WHAT IS RECURSION?
  561.  
  562.              Recursion  is another of those  programming  techniques
  563.         that  seem very intimidating the first time you come  across
  564.         it,  but  if  you will load and display the example  program
  565.         named RECURSON.C, we will take all of the mystery out of it.
  566.         This  is probably the simplest recursive program that it  is
  567.         possible  to write and it is therefore a stupid  program  in
  568.         actual  practice,  but for purposes of illustration,  it  is
  569.         excellent.
  570.  
  571.              Recursion  is  nothing more than a function that  calls
  572.         itself.  It is therefore in a loop which must have a way  of
  573.         terminating.   In the program on your monitor, the  variable
  574.         "index"  is  set to 8, and is used as the  argument  to  the
  575.         function  "count_dn".   The function simply  decrements  the
  576.         variable, prints it out in a message, and if the variable is
  577.         not zero, it calls itself, where it decrements  the variable
  578.         again, prints it, etc. etc. etc. Finally, the variable  will
  579.         reach  zero,  and the function will not call  itself  again.
  580.         Instead, it will return to the prior time it called  itself,
  581.         and  return again, until finally it will return to the  main
  582.         program and from there return to DOS.
  583.  
  584.  
  585.  
  586.                                   Page 39
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.             Chapter 5 - Functions, variables, and prototypes
  597.  
  598.  
  599.              For  purposes  of understanding you can think of it  as
  600.         having 8 copies of the function "count_dn" available and  it
  601.         simply  called all of them one at a time,  keeping track  of
  602.         which  copy it was in at any given time.   That is not  what
  603.         actually  happened,  but it is a reasonable illustration for
  604.         you to begin understanding what it was really doing.
  605.  
  606.                               WHAT DID IT DO?
  607.  
  608.              A  better explanation of what actually happened  is  in
  609.         order.   When you called the function from itself, it stored
  610.         all  of the variables and all of the internal flags it needs
  611.         to  complete the function in a block  somewhere.   The  next
  612.         time it called itself,  it did the same thing,  creating and
  613.         storing  another  block of everything it needed to  complete
  614.         that  function call.   It continued making these blocks  and
  615.         storing them away until it reached the last function when it
  616.         started  retrieving the blocks of data,  and using  them  to
  617.         complete  each function call.   The blocks were stored on an
  618.         internal part of the computer called the "stack".  This is a
  619.         part  of  memory carefully organized to store data  just  as
  620.         described above.  It is beyond the scope of this tutorial to
  621.         describe the stack in detail,  but it would be good for your
  622.         programming  experience to read some material describing the
  623.         stack.   A stack is used in nearly all modern computers  for
  624.         internal housekeeping chores.
  625.  
  626.              In using recursion,  you may desire to write a  program
  627.         with  indirect recursion as opposed to the direct  recursion
  628.         described  above.    Indirect  recursion  would  be  when  a
  629.         function  "A"  calls the function "B",  which in turn  calls
  630.         "A",  etc.   This is entirely permissible,  the system  will
  631.         take  care of putting the necessary things on the stack  and
  632.         retrieving  them when needed again.   There is no reason why
  633.         you  could not have three functions calling each other in  a
  634.         circle,  or four,  or five,  etc.   The C compiler will take
  635.         care of all of the details for you.
  636.  
  637.              The thing you must remember about recursion is that  at
  638.         some  point,  something  must  go to  zero,  or  reach  some
  639.         predefined  point to terminate the loop.   If not,  you will
  640.         have  an  infinite  loop,  and the stack will  fill  up  and
  641.         overflow,  giving  you  an error and  stopping  the  program
  642.         rather abruptly.
  643.  
  644.              Compile  and  run  this  program.   If  you  left   the
  645.         prototype  checking on from the last program you  will  find
  646.         several  "warnings"  in the message window when  you  return
  647.         to  the  Integrated  Environment.  Study  these  for  a  few
  648.         minutes.  One of the suggested exercises at the end of  this
  649.  
  650.  
  651.  
  652.                                   Page 40
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.             Chapter 5 - Functions, variables, and prototypes
  663.  
  664.  
  665.         chapter is to modify this program to eliminate the prototype
  666.         warnings.
  667.  
  668.                         ANOTHER EXAMPLE OF RECURSION
  669.  
  670.              The  program  named  BACKWARD.C is another  example  of
  671.         recursion,  so load it and display it on your screen.   This
  672.         program  is  similar to the last one except that it  uses  a
  673.         character array.  Each successive call to the function named
  674.         "forward_and_backwards" causes one character of the  message
  675.         to  be printed.  Additionally, each time the function  ends,
  676.         one of the characters is printed again, this time  backwards
  677.         as the string of recursive function calls is retraced.
  678.  
  679.              This  program  uses  the "modern"  method  of  function
  680.         definition  and  includes full prototype  definitions.   The
  681.         "modern"  method of function definition moves the  types  of
  682.         the  variables into the parentheses along with the  variable
  683.         names  themselves.   The  final  result  is  that  the  line
  684.         containing   the   function  name  looks   more   like   the
  685.         corresponding line in Pascal, Modula 2, or Ada.
  686.  
  687.              Don't worry about the character array defined in line 9
  688.         or  the  other  new  material  presented  here.   After  you
  689.         complete chapter 7 of this tutorial,  this program will make
  690.         sense.   It  was  felt that introducing a second example  of
  691.         recursion was important so this file is included here.
  692.  
  693.              Compile  and run this program with  prototype  checking
  694.         on, and observe the results.
  695.  
  696.                  HOW TO WORK AROUND THE TURBO C (v1.0) BUG
  697.  
  698.              Load and display the program named FLOATSQ2.C which  is
  699.         an  exact copy of the program FLOATSQ.C which we  considered
  700.         earlier with prototyping added.  The return of the erroneous
  701.         zeros  is now repaired and the correct values are  returned.
  702.         Apparently,  when  the  coders of Turbo C  at  Borland  were
  703.         testing this compiler, they used prototyping and never found
  704.         this bug.  The use of prototyping is a good practice for all
  705.         C  programmers  to  get into.  Note that if  you  are  using
  706.         version 1.5 of TURBO C this file will also compile correctly
  707.         so you can use either method of programming.  It would be to
  708.         your advantage to learn the newer method using prototypes.
  709.  
  710.              Several things should be mentioned about this  program.
  711.         First, the word "float" at the beginning of lines 27 and  35
  712.         indicate to the compiler that these functions are  functions
  713.         that return "float" type values.  Also, since the prototypes
  714.         are  given before "main", the functions are not required  to
  715.         be identified in line 12 as they were in line 7 of FLOATSQ.C
  716.  
  717.  
  718.                                   Page 41
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.             Chapter 5 - Functions, variables, and prototypes
  729.  
  730.  
  731.         earlier  in this chapter.  They can be included in line  12,
  732.         but they are not required to be.
  733.  
  734.              Notice  also that the type of the variable  "inval"  is
  735.         included  within  the parentheses in line 27.  It  would  be
  736.         very educational for you to modify this program so that  you
  737.         included  a  call  to "sqr" with a variable  of  type  "int"
  738.         within  the  parentheses to see what kind of a  warning  you
  739.         would  get.   Do  the  same thing  in  the  program  without
  740.         prototype checking, FLOATSQ.C.
  741.  
  742.  
  743.  
  744.         PROGRAMMING EXERCISES
  745.  
  746.         1.   Rewrite  TEMPCONV.C,  from an earlier chapter, and move
  747.              the temperature calculation to a function.
  748.  
  749.         2.   Write a program that writes your name on the monitor 10
  750.              times by calling a function to do the writing. Move the
  751.              called function ahead of the "main" function to see  if
  752.              the Turbo C compiler will allow it.
  753.  
  754.         3.   Add  prototyping  to the program  named  RECURSON.C  to
  755.              eliminate the warnings.
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.                                   Page 42
  785.